/**
 * Created by Ji.wenjiang on 2016/8/30.
 */


angular.module('inspinia')
  .controller('contourImageList', function ($scope, imageValues, $log, $http) {
    init();
    function init() {
      $scope.visible1 = true;
      $scope.visible2 = true;
      $scope.visible3 = true;
      $scope.visible4 = true;
      $scope.inputName = '患者姓名';
      $scope.inputPlace = '请输入查询姓名';
      $scope.starttime1 = '上传起始日期';
      $scope.endtime1 = '上传截止日期';
      $scope.inputName1 = '患者ID';
      $scope.starttime = '拍摄起始日期';
      $scope.endtime = '拍摄截止日期';
      var data = {
        userid: '18',
        havereport: -1,
        haveask: -1,
        curpage: 1,
        pagesize: 1000
      }
      var transform = function (data) {
        return $.param(data);
      }

      // // lmViewer.ctrl.initImageList(data, success, fail);
      // lm.ajax.post(_obj, success, fail)
      $http.post( lm.environment().imageCloudUrl+'/service/image/list', data, {
          headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
          transformRequest: transform
        })
        .success(function (data) {

          var message=JSON.parse(data.message);
          $scope.persons1 = message;
          $scope.persons = $scope.persons1;
            setTimeout(function () {
              $('#mainTable').trigger('footable_redraw');
            }, 100);

        })
      $scope.mFilter_noImage = function (e) {
        return e.modality != "noImage";
      }

    }

    $scope.viewer = function (imageid, patientid, studyid) {
      // $scope.set = message[0];

      var url = '?imageid=' + imageid + '&patientID='
        + patientid + '&' + 'studyUID=' + studyid
        + '&reportFlag=0' + '&type=jpg'

      window.location.href = '/#/viewer/contouring02' + url;
      window.location.reload();


    }

    //search image
    $scope.search = {};
    //搜索
    $scope.searchForInput = function () {
      var flag1 = false;
      var flag2 = false;
      if (($scope.searchTerm == undefined || $scope.searchTerm == '') && ($scope.searchTerm1 == undefined || $scope.searchTerm1 == '')) {
        flag1 = true;
      }
      if ($scope.visible1 == true && $scope.visible2 == true && $scope.visible3 == true && $scope.visible4 == true) {
        flag2 = true;
      }
      if (flag1 && flag2 == true) {
        $scope.persons=$scope.persons1;
        return;
      } else {
        $scope.searchStartTime = $scope.visible1 == false ? handleTime($("#studystartdate").val()) : -1;
        $scope.searchEndTime = $scope.visible2 == false ? handleTime($("#studyenddate").val()) : 99999999;
        $scope.searchStartTime1 = $scope.visible3 == false ? handleTime($("#studystartdate1").val()) : -1;
        $scope.searchEndTime1 = $scope.visible4 == false ? handleTime($("#studyenddate1").val()) : 99999999;
        $scope.persons = [];
        angular.forEach($scope.persons1, function (data, index, array) {
          var namecheck = false;
          var idcheck = false;
          var timecheck1 = false;
          var timecheck2 = false;
          var patientname = data.patientname.toLowerCase();
          var patientid = data.patientid.toLowerCase();
          if (patientname.indexOf($scope.handleTerm) != -1 || $scope.handleTerm == undefined) {
            namecheck = true;
          }
          if (patientid.indexOf($scope.handleTerm1) != -1 || $scope.handleTerm1 == undefined) {
            idcheck = true;
          }
          if ($scope.searchStartTime1 <= parseInt(data.uploaddate) && $scope.searchEndTime1 >= parseInt(data.uploaddate)) {
            timecheck1 = true;
          }
          if ($scope.searchStartTime <= handleTime(data.studydate) && $scope.searchEndTime >= handleTime(data.studydate)) {
            timecheck2 = true;
          }
          if (namecheck && timecheck1 && timecheck2 && idcheck) {
            $scope.persons.push(data);
          }
        })
      }


    }
  })
